package com.health.tools;

import android.content.Context;
import android.content.res.AssetManager;

import com.health.model.PeopleType;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Calendar;

/**
 * @Author: xiaoxin
 * @CreateDate: 2019/12/16 9:47
 * @Description: 描述:
 * @UpdateRemark: 更新说明
 */
public class AssetUtil {

    /**
     * 读取asset下json文件
     * @param fileName
     * @param context
     * @return
     */
    public static String getJson(String fileName, Context context) {
        //将json数据变成字符串
        StringBuilder stringBuilder = new StringBuilder();
        try {
            //获取assets资源管理器
            AssetManager assetManager = context.getAssets();
            //通过管理器打开文件并读取
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    /**
     * 根据身份证计算年龄
     * @param idcard
     * @return
     */
    public static Integer idCardToAge(String idcard) {
        Integer selectYear = Integer.valueOf(idcard.substring(6, 10));         //出生的年份
        Integer selectMonth = Integer.valueOf(idcard.substring(10, 12));       //出生的月份
        Integer selectDay = Integer.valueOf(idcard.substring(12, 14));         //出生的日期
        Calendar cal = Calendar.getInstance();
        Integer yearMinus = cal.get(Calendar.YEAR) - selectYear;
        Integer monthMinus = cal.get(Calendar.MONTH) + 1 - selectMonth;
        Integer dayMinus = cal.get(Calendar.DATE) - selectDay;
        Integer age = yearMinus;
        if (yearMinus < 0) {
            age = 0;
        } else if (yearMinus == 0) {
            age = 0;
        } else if (yearMinus > 0) {
            if (monthMinus == 0) {
                if (dayMinus < 0) {
                    age = age - 1;
                }
            } else if (monthMinus > 0) {
                age = age + 1;
            }
        }
        return age;
    }

    public static String getPeopleType(String peopleType) {
        String[] split = peopleType.split(",");
        StringBuilder stringBuilder = new StringBuilder();
        /** 1:一般人群，2：儿童，3：孕产妇，4：老年人，5：重精神病，6：高血压，7：糖尿病，8：冠心病，9：脑卒中 10残疾人    默认值 1
         11：肺结核  ；12：建档立卡贫困户，13：计划生育特殊家庭  14：卡外贫困户  15：低保户， 默认值1
         * 年龄分类标准：儿童：<=6;孕妇：孕龄标准(age >= 14 && age <= 55);老年人：>=65
         */
        for (int i = 0; i < split.length; i++) {
            switch (split[i]) {
                case "1":
                    stringBuilder.append(PeopleType.YBRQ_NAME + ",");
                    break;
                case "2":
                    stringBuilder.append(PeopleType.CHILD_NAME + ",");
                    break;
                case "3":
                    stringBuilder.append(PeopleType.WOMAN_NAME + ",");
                    break;
                case "4":
                    stringBuilder.append(PeopleType.OLD_NAME + ",");
                    break;
                case "5":
                    stringBuilder.append(PeopleType.JSB_NAME + ",");
                    break;
                case "6":
                    stringBuilder.append(PeopleType.GXY_NAME + ",");
                    break;
                case "7":
                    stringBuilder.append(PeopleType.TNB_NAME + ",");
                    break;
                case "8":
                    stringBuilder.append(PeopleType.GXB_NAME + ",");
                    break;
                case "9":
                    stringBuilder.append(PeopleType.NCZ_NAME + ",");
                    break;
                case "10":
                    stringBuilder.append(PeopleType.CJR_NAME + ",");
                    break;
                case "11":
                    stringBuilder.append(PeopleType.FJH_NAME + ",");
                    break;
                case "12":
                    stringBuilder.append(PeopleType.JDLKPKH_NAME + ",");
                    break;
                case "13":
                    stringBuilder.append(PeopleType.JHSYTSJT_NAME + ",");
                    break;
                case "14":
                    stringBuilder.append(PeopleType.KWPKH_NAME + ",");
                    break;
                case "15":
                    stringBuilder.append(PeopleType.DBH_NAME + ",");
                    break;
            }
        }
        return stringBuilder.toString().substring(0,stringBuilder.toString().length()-1);
    }

}
